Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 10x 10x 10x 9x 9x 9x 6x 6x 6x 6x 1x | import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth";
import { PreferenceController } from "@/server/api/preferences";
import { requireApiCollectionAccess } from "@/server/dal/collection-access";
import { getDb } from "@/lib/db";
import { lockCollectionCurator, withTransaction } from "@/lib/transactions";
import { errorResponse } from "@/server/api/http-errors";
export async function POST(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params;
const user = await requireAuth();
const db = getDb();
const access = await requireApiCollectionAccess(db, id, user.id);
if (access instanceof NextResponse) return access;
// Same tx + same (collection, curator) advisory lock the decision writers
// take. recalculatePreferences reads the collection's live items then
// bulk-upserts preference rows for them; on the raw db those two statements
// autocommit separately, so a purge could delete an item in between and the
// upsert would die on 23503 (preferences.item_id has no ON DELETE CASCADE).
// The purge takes this lock for every curator — this side must take it too.
const prefs = await withTransaction(db, async (tx) => {
await lockCollectionCurator(tx, id, user.id);
return PreferenceController.fromDb(tx).recalculatePreferences(id, user.id);
});
return NextResponse.json({ message: "Preferences recalculated", preferences: prefs });
} catch (error) {
return errorResponse(error);
}
}
|